// JavaScript Document

var gLocalSearch;
var gMap;
var gInfoWindow;
var gSearchForm;

var accuracyToZoomLevel = [
	1,  // 0 - Unknown location
	5,  // 1 - Country
	6,  // 2 - Region (state, province, prefecture, etc.)
	8,  // 3 - Sub-region (county, municipality, etc.)
	11, // 4 - Town (city, village)
	13, // 5 - Post code (zip code)
	15, // 6 - Street
	16, // 7 - Intersection
	17, // 8 - Address
	17  // 9 - Premise
];
var zoomLevel = accuracyToZoomLevel[4] || 1;

// italia
var MYlatlng = new google.maps.LatLng(41.87194, 12.56738);
var latlng = MYlatlng;

var MYmarker;

// coordinate della destinazione o false
var target = false;

// Set up the map and the local searcher.
function initMap() {
	
	// Initialize the map with default UI.
	var myOptions = {
		zoom: 5,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.HYBRID
	}
	
	gMap = new google.maps.Map(document.getElementById("map"), myOptions);
	
	MYmarker = new google.maps.Marker({
		position: MYlatlng, 
		map: gMap,
		draggable:true,
		title:"Ciao, sei qui!"});
	
	// Create one InfoWindow to open when a marker is clicked.
	gInfoWindow = new google.maps.InfoWindow;
	google.maps.event.addListener(gInfoWindow, 'closeclick', function() {
		unselectMarkers();
	});
	
	// Initialize the local searcher
	//gLocalSearch = new GlocalSearch();
	//gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);
	//GSearch.setOnLoadCallback(initMap);

	// ricerca api v3
	
	gLocalSearch = new google.maps.places.PlacesService(gMap);

	// georeferenziazione di un indirizzo
	geocoder = new google.maps.Geocoder();
	// funzioni contenute dentro georeferenziazione.js
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(success, error);
	} else {
		error('not supported');
	}
	
	// quando il marker viene spostato, aggiorno la posizione dell'utente
	google.maps.event.addListener(MYmarker, 'position_changed', function(){
		MYlatlng = MYmarker.getPosition();
		});
	
	// init del calcolo del percorso
	initialize_traceroute();
	init_autocompletamento();
}
 